package com.witframework.baseapp.security.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.witframework.baseapp.security.Resource;
import com.witframework.baseapp.security.dao.ResourceDao;
import com.witframework.baseapp.security.service.ResourceService;
import com.witframework.cache.WitCache;
import com.witframework.cache.WitCacheFactory;
import com.witframework.core.logic.BaseService;

@Service("resourceService")
public class ResourceServiceImpl extends BaseService implements ResourceService{
	ResourceDao<Resource> resourceDao;
	
	@Autowired
	public void setDao(ResourceDao<Resource> resourceDao) {
		this.resourceDao = resourceDao;
		super.setDao(resourceDao);
	}
	private Map getFilterForIsUse(){
		Map filter=new HashMap();
		filter.put("isUse","Y");
		return filter;
	}
	public List<Resource> getAllGuestResource() {
		Map filter=getFilterForIsUse();
		filter.put("isGuest","Y");		
		return super.findBy(filter);		
	}

	public List<Resource> getAllPublicResource() {
		Map filter=getFilterForIsUse();
		filter.put("isPublic","Y");		
		return super.findBy(filter);	
	}

	public List<Resource> getAllMenuResource() {
		Map filter=getFilterForIsUse();
		filter.put("resourceType","MENU");		
		return super.findBy(filter);			
	}

	public List<Resource> getAllNavResource() {
		Map filter=getFilterForIsUse();
		filter.put("isNav","Y");		
		return super.findBy(filter);	
	}
	public List<Resource> getMenuResource(String parent) {
		Map filter=getFilterForIsUse();
		filter.put("resourceType","MENU");	
		filter.put("parentId",parent);	
		return super.findBy(filter);	
	}
	public boolean isGuestResource(String resourceValue){
		WitCache resourceCache=WitCacheFactory.getCache(Resource.class);
		List resouece=resourceCache.find("WHERE isUse='Y' AND isGuest='Y' AND resourceValue LIKE '%"+resourceValue+"'");
		return resouece.size()>0;
	}
	public boolean isPublicResource(String resourceValue){	
		WitCache resourceCache=WitCacheFactory.getCache(Resource.class);
		List resouece=resourceCache.find("WHERE isUse='Y' AND isPublic='Y' AND resourceValue LIKE '%"+resourceValue+"'");
		return resouece.size()>0;
	}
	public boolean isSecureResource(String resourceValue) {
		WitCache resourceCache=WitCacheFactory.getCache(Resource.class);
		List resouece=resourceCache.find("WHERE isUse='Y' AND isGuest='N' AND resourceValue LIKE '%"+resourceValue+"'");
		return resouece.size()>0;
	}
}
